﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HandRise.MBHOP.Controls;
using Handrise.CDCable.Mobile.Business;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmFlawEdit : FrmBase
    {
        private Flaw flaw = null;
        private int editType = 0;  //0:新增 1:修改
        private Device device = null;
        private List<FlawPhenomenon> flawPhenomenons = new List<FlawPhenomenon>();
        private FlawService flawService = new FlawService();
        

        public FrmFlawEdit()
        {
            InitializeComponent();
            ttlbrhead.Text = ResourceImage.缺陷管理;
            ilstbcup.Images.Add(ResourceImage.s_保存);
            ilstbcup.Images.Add(ResourceImage.s_删除);
            ilstbcup.Images.Add(ResourceImage.s_上报);
            ilstbcup.Images.Add(ResourceImage.s_返回01);

            ilstbcdown.Images.Add(ResourceImage.s_保存02);
            ilstbcdown.Images.Add(ResourceImage.s_删除02);
            ilstbcdown.Images.Add(ResourceImage.s_上报02);
            ilstbcdown.Images.Add(ResourceImage.s_返回02);
        }

        private void FrmFlawEdit_Load(object sender, EventArgs e)
        {
            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

            tbTitle.Text = editType == 0 ? "新增缺陷" : "修改缺陷";

            if (flaw == null)
            {
                flaw = new Flaw();
                flaw.FlawID = Guid.NewGuid().ToString();
                flaw.DealState = "草稿";
                flaw.DisCoverDateTime = DateTime.Now;
                flaw.DisCoverer = AppLogic.LoginUser.RealName;
                flaw.Source = "PDA";
                flaw.UserName = AppLogic.LoginUser.UserName;
                flaw.RealName = AppLogic.LoginUser.RealName;
                flaw.UnitName = AppLogic.LoginUser.UnitName;
                flaw.ReportState = "未上报";
            }

            FlawPhenomenonService flawPhenomenonService = new FlawPhenomenonService();
            if (device != null)
            {
                flaw.DeviceID = device.DeviceID;
                flaw.DeviceName = device.Name;
                flaw.DeviceType = device.DeviceType;

                //设置设备大类
                EquipmentTypeService equipTypeService = new EquipmentTypeService();
                flaw.DeviceLargeType = equipTypeService.GetDeviceLargeType(device.DeviceType);

                //获取缺陷现象
                flawPhenomenons = flawPhenomenonService.GetFlawPhenomenonByDeviceType(device.DeviceType);

                //设置缺陷部位
                foreach (FlawPhenomenon flawPhenomenon in flawPhenomenons)
                {
                    if (!cbFlawPart.Items.Contains(flawPhenomenon.FBIGPART))
                        cbFlawPart.Items.Add(flawPhenomenon.FBIGPART);
                }
            }


            tbDeviceName.Text = flaw.DeviceName;
            cbFlawPart.Text = flaw.Part;
            cbFlawLevel.Text = flaw.Level;

            if (cbFlawPart.Text != "")
            {
                //设置缺陷现象
                cbFlawPhenmenon.DisplayMember = "FLAWPHENOMENON";
                cbFlawPhenmenon.DataSource = flawPhenomenons.Where(t => t.FBIGPART == cbFlawPart.Text).ToList<FlawPhenomenon>();
            }

            cbFlawPhenmenon.Text = flaw.Phenomenon;

            cbFlawPart.SelectedIndexChanged += new EventHandler(cbFlawPart_SelectedIndexChanged);
            cbFlawPhenmenon.SelectedIndexChanged += new EventHandler(cbFlawPhenmenon_SelectedIndexChanged);
            cbFlawLevel.SelectedIndexChanged += new EventHandler(cbFlawLevel_SelectedIndexChanged);

        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            if (PriorForm == "FrmQueryDevice" || PriorForm == "FrmSelectDevice")
            {
                //设备选择进入
                device = (Device)Parameter[0];
                object[] oldParameters = (object[])Parameter[1];
                editType = (int)oldParameters[1];
                flaw = (Flaw)oldParameters[2];
            }
            else
            {
                editType = (int)Parameter[0];
                flaw = (Flaw)Parameter[1];
                device = (Device)Parameter[2];
            }
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        public void LoadCacheData()
        {
            editType = (int)Caches[0];
            flaw = (Flaw)Caches[1];
            device = (Device)Caches[2];
        }

        private void bcOpreate_OnButtonClick(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 0:  //保存
                    if(SaveFlaw())
                        mssdlgConfirm.ShowDialog(this, "保存成功", DialogType.Confirm, FormManage.form_Deactivate);
                    break;
                case 1: //删除
                    DeleteFlaw();
                    break;
                case 2: //上报
                    ReportFlaw();
                    break;
                case 3: //返回
                    FormManage.GotoNext("FrmFlawList", null, null, true);
                    break;

            }
        }

        /// <summary>
        /// 保存缺陷
        /// </summary>
        private bool SaveFlaw()
        {
            bool re = false;

            //设置缺陷标题
            flaw.Title = flaw.Part + flaw.Phenomenon;

            //保存缺陷
            switch (editType)
            {
                case 0:   //新增
                    re = flawService.AddFlaw(flaw);
                    break;
                case 1:   //编辑
                    re =flawService.UpdateFlaw(flaw);
                    break;
                default:
                    break;
            }

            if(re)
              editType = 1;

            return re;

        }

        /// <summary>
        /// 删除缺陷
        /// </summary>
        private void DeleteFlaw()
        {
            if (editType == 1)
            {
                if (mssdlgSelect.ShowDialog(this, "确认删除当前缺陷？", DialogType.Select, FormManage.form_Deactivate) == MessageDialogResult.Button1)
                {
                    flawService.DeleteFlaw(flaw.FlawID);
                    FormManage.GotoNext("FrmFlawList", null, null, true);
                }
            }

        }

        /// <summary>
        /// 上报缺陷
        /// </summary>
        private void ReportFlaw()
        {
            if (AppLogic.netService.CurrentNetType == NetType.None)
            {
                mssdlgConfirm.ShowDialog(this, "请先连通网络再进行上报", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            if(flaw.ReportState != "未上报")
            {
                mssdlgConfirm.ShowDialog(this, "当前缺陷" + flaw.ReportState, DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            //先保存当前缺陷
            SaveFlaw();

            flawService.ReportFlawToServer(flaw.FlawID);
            

        }

        private void btnDeviceName_Click(object sender, EventArgs e)
        {
            FormManage.GotoNext("FrmQueryDevice", new object[] {"缺陷设备选择", new object[] {"FrmFlawEdit",editType,flaw} }, new object[] {editType, flaw, device }, false);
        }

        void cbFlawPart_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbFlawPart.Text != "")
            {
                //设置缺陷现象
                cbFlawPhenmenon.DisplayMember = "FLAWPHENOMENON";
                cbFlawPhenmenon.DataSource = flawPhenomenons.Where(t => t.FBIGPART == cbFlawPart.Text).ToList<FlawPhenomenon>();
            }

            if (flaw != null)
                flaw.Part = cbFlawPart.Text;
        }


        void cbFlawLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flaw != null)
                flaw.Level = cbFlawLevel.Text;
        }

        void cbFlawPhenmenon_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (flaw != null)
                flaw.Phenomenon = cbFlawPhenmenon.Text;
        }

        private void btnPhoto_Click(object sender, EventArgs e)
        {
            //如果没有保存缺陷，先保存
            SaveFlaw();

            if (flaw == null)
                return;

            LinkFileService linkFileService = new LinkFileService();
            LinkFile linkFile = linkFileService.TaskPicture(flaw.FlawID);
            if (linkFile == null)
                return;

            FormManage.GotoNext("FrmCamera", new object[] { "FrmFlawEdit", linkFile }, new object[] { editType, flaw, device }, false);

        }

        private void btnRecord_Click(object sender, EventArgs e)
        {
            //如果没有保存缺陷，先保存
            SaveFlaw();

            if (flaw == null)
                return;

            FormManage.GotoNext("FrmRecording", new object[] { "FrmFlawEdit", flaw.FlawID }, new object[] { editType, flaw, device }, false);

        }


    }
}